package com.bonuo.crm.service.impl;

import com.bonuo.crm.domain.Activity;
import com.bonuo.crm.domain.Kpi;
import com.bonuo.crm.domain.Process;
import com.bonuo.crm.domain.Visit;
import com.bonuo.crm.mapper.KpiMapper;
import com.bonuo.crm.service.KpiService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class KpiServiceImpl implements KpiService {
    @Autowired
    private KpiMapper kpiMapper;
    @Override
    public List<Kpi> selectKpiList(Kpi kpi) {
        return kpiMapper.selectKpiList(kpi);
    }

    @Override
    public Integer insertKpi(Kpi kpi) {
        return kpiMapper.insertKpi(kpi);
    }

    @Override
    public AjaxResult updateKpi(Kpi kpi) {
        if(kpi.getDisciplineScore() == null || kpi.getWeeklyScore() == null || kpi.getMediaScore() == null){
            return AjaxResult.error("媒体/纪律/周报分数请填写完整");
        }
        kpi.setTotalScore(kpi.getCollectScore()+kpi.getVisitScore()+kpi.getMerchandiserScore()+kpi.getChangeScore()+kpi.getActivityScore()+kpi.getTenderScore()+kpi.getBusinessScore()+kpi.getWeeklyScore()+kpi.getDisciplineScore()+kpi.getMediaScore());
        return AjaxResult.success(kpiMapper.updateKpi(kpi));
    }

    @Override
    public Integer deleteKpiById(Integer id) {
        return kpiMapper.deleteKpiById(id);
    }

    @Transactional
    @Override
    public AjaxResult countEveryNum(Kpi kpi) {
        //查看数据库有没有信息，有的话就不生成了
//        Integer count = kpiMapper.countByMonth(kpi.getExamineDate());
//        if(count > 0){
//            return AjaxResult.error(kpi.getExamineDate()+"月份已生成数据，不能再次生成");
//        }

        List<SysUser> userList = kpiMapper.selectAllUser();

        boolean flag = false;
        for (SysUser sysUser : userList) {
            //遍历用户表把用户名称赋值到考核人
            kpi.setExamineName(sysUser.getUserName());
            //项目信息收集条数
            Integer projectCollectCount = kpiMapper.countProjectCollect(kpi);
            //拜访记录条数
            Integer visitCount = kpiMapper.countVisit(kpi);
            //销售跟单条数
            Integer salesMerchandiserCount = kpiMapper.countSalesMerchandiser(kpi);
            //销售机会条数
            Integer salesChangeCount = kpiMapper.countSalesChange(kpi);
            //服务大赛、展会活动分数
            Integer projectActivityCount = kpiMapper.countProjectActivity(kpi);
            //商务标书制作及投标分数
            Integer projectTenderCount = kpiMapper.countProjectTender(kpi);
            //协助伙伴处理商务事宜分数
            Integer projectBusinessCount = kpiMapper.countProjectBusiness(kpi);
            //周报条数
//            Integer weeklyCount = kpiMapper.countWeekly(kpi);
            //如果所有的数量都是0 那么代表该用户没有数据，跳过该用户
//            if(projectCollectCount ==0 && visitCount==0 && salesMerchandiserCount==0 && salesChangeCount==0 && projectActivityCount==0 && projectTenderCount==0 && projectBusinessCount==0){
//                continue;
//            }
            //项目信息收集分数重新赋值
            if(projectCollectCount < 4){
                projectCollectCount = 0;
            } else if (projectCollectCount<=5) {
                projectCollectCount = 8;
            } else if (projectCollectCount<=7) {
                projectCollectCount = 15;
            }else {
                projectCollectCount = 20;
            }

            //拜访记录分数重新赋值
            if(visitCount < 12 ){
                visitCount = 0;
            } else if (visitCount<=19) {
                visitCount = 12;
            } else if (visitCount<=24){
                visitCount = 20;
            } else {
                visitCount = 25;
            }

            //销售跟单分数重新赋值
            if(salesMerchandiserCount < 4){
                salesMerchandiserCount = 0;
            } else if (salesMerchandiserCount <= 5) {
                salesMerchandiserCount = 8;
            } else if (salesMerchandiserCount<=7) {
                salesMerchandiserCount = 15;
            } else {
                salesMerchandiserCount = 20;
            }

            //销售机会分数重新赋值
            if(salesChangeCount == 1){
                salesChangeCount = 3;
            } else if (salesChangeCount == 2) {
                salesChangeCount = 9;
            } else if (salesChangeCount == 3) {
                salesChangeCount = 18;
            } else if (salesChangeCount >= 4) {
                salesChangeCount = 27;
            } else{
                salesChangeCount = 0;
            }

            //插入数据到kpi表
            kpi.setExamineName(sysUser.getNickName());
            kpi.setCollectScore(projectCollectCount);
            kpi.setVisitScore(visitCount);
            kpi.setMerchandiserScore(salesMerchandiserCount);
            kpi.setChangeScore(salesChangeCount);
            kpi.setActivityScore(projectActivityCount);
            kpi.setTenderScore(projectTenderCount);
            kpi.setBusinessScore(projectBusinessCount);
//            kpi.setWeeklyScore(weeklyCount);
            kpi.setTotalScore(projectCollectCount + visitCount + salesMerchandiserCount + salesChangeCount + projectActivityCount + projectTenderCount + projectBusinessCount);
            kpiMapper.insertKpi(kpi);
            if(! flag){
                flag = true;
            }
        }

        return flag?AjaxResult.success(kpi.getExamineDate()+"数据生成成功"):AjaxResult.error(kpi.getExamineDate()+"月份没有记录信息");
    }
}
